import { Component, Injector, Inject, Optional, OpaqueToken, OnInit, ViewChild, Output, EventEmitter,Input } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AppComponentBase } from '@shared/app-component-base';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import * as moment from 'moment';
import {
  API_BASE_URL,
  PagedResultDtoOfAgentApplyListDto,
  AgentServiceProxy,
  EntityDto
} from '@shared/service-proxies/service-proxies';
import { AppConsts } from '@shared/AppConsts';
@Component({
  moduleId: module.id,
  selector: 'app-agent-noconfirmed-list',
  templateUrl: 'agent-noconfirmed-list.component.html',
  styleUrls: ['agent-noconfirmed-list.component.scss'],
  animations: [appModuleAnimation()]
})
export class AgentNoconfirmedListComponent extends AppComponentBase implements OnInit {
  @Output() confirmed: EventEmitter<any> = new EventEmitter<any>();
  agentList: PagedResultDtoOfAgentApplyListDto = null;
  downloadUrl: string = AppConsts.remoteServiceBaseUrl + '/file/download/';
  currentPage: number = 1;
  condition: GetNoConfirmedAgentCondition;
  constructor(
    injector: Injector,
    private _agentService: AgentServiceProxy,
    private route: ActivatedRoute
  ) {
    super(injector);
  }

  ngOnInit(): void {
    this.condition = new GetNoConfirmedAgentCondition();
    this.condition.maxResultCount = 10;
    this.getAgents();
  }

  getAgents(): void {
    this._agentService.getNoConfirmedApplyAsync(this.condition.agentType,
      this.condition.operator,
      this.condition.keyword,
      this.condition.sorting,
      this.condition.skipCount,
      this.condition.maxResultCount
    ).subscribe((result: PagedResultDtoOfAgentApplyListDto) => {
      this.agentList = result;
    });
  }
  noNeedConfirm(id: number): void {
    abp.message.confirm('跳过用户确认直接设置为店长? ', '请确认', (result: boolean) => {
      if (result) {
        const input = new EntityDto();
        input.id = id;
        this._agentService.noNeedConfirmApplyAsync(input).subscribe(() => {
          abp.notify.success('操作成功!');
          this.getAgents();
          this.confirmed.emit(null);
        });
      }
    });
  }

  revoke(id: number): void {
    abp.message.confirm('确定要撤销这个待确认的店长?', '请确认', (result: boolean) => {
      if (result) {
        const input = new EntityDto();
        input.id = id;
        this._agentService.revokeAgentApplyAsync(input).subscribe(() => {
          abp.notify.success('操作成功!');
          this.getAgents();
        });
      }
    });
  }

  pageChanged($event: any): void {
    this.condition.skipCount = this.condition.maxResultCount * $event.page;
    this.getAgents();
  }
}


export class GetNoConfirmedAgentCondition {
  agentType: string;
  operator: string;
  maxResultCount: number;
  skipCount: number;
  sorting: string;
  keyword: string;
}
